package com.gzl.demo.controller;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.gzl.demo.entity.ShopThirdPartyTableCode;
import com.gzl.demo.utils.ExcelUtil;
import com.gzl.demo.utils.QRCodeUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/code")
public class CodeController {
    /**
     *  生成二维码
     * @param type 二维码的类型，为了演示效果，1跳百度的，2是跳京东

     * */
    @GetMapping(value = "/activity/create/activity-code")
    public void getCode(int type , HttpServletResponse response) throws IOException {
        // 设置响应流信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        OutputStream stream = response.getOutputStream();

        //type是1，生成活动详情、报名的二维码，type是2，生成活动签到的二维码
        String content = (type == 1 ? "http://www.baidu.com" : "http://www.jd.com");
        //获取一个二维码图片
        BitMatrix bitMatrix = QRCodeUtils.createCode(content);
        //以流的形式输出到前端
        MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream);
    }

    /**
     *  导出excel
     * @param

     * */
    @GetMapping(value = "/export")
    public void exportExcel(HttpServletResponse httpServletResponse) throws IOException {
        List<ShopThirdPartyTableCode> shopThirdPartyTableCodes =new ArrayList<>();
        ShopThirdPartyTableCode shopThirdPartyTableCode=new ShopThirdPartyTableCode();
        shopThirdPartyTableCode.setShopId(25l);
        shopThirdPartyTableCode.setShopName("hhaha");
        shopThirdPartyTableCode.setTableName("5");
        shopThirdPartyTableCode.setTableCodeurl("sadfadsf");
        shopThirdPartyTableCodes.add(shopThirdPartyTableCode);
        try {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String fileName = "第三方桌台二维码--" + s.format(new Date()) + ".xls";
            File saveFile = new File(fileName);
            OutputStream out = new FileOutputStream(saveFile);
            String[] headers = {"店铺ID", "店铺名称", "第三方桌台号", "桌台付款二维码"};
            List<List<String>> data = new ArrayList<List<String>>();
            for (int i = 0; i < shopThirdPartyTableCodes.size(); i++) {
                List<String> list1 = new ArrayList<>();
                ShopThirdPartyTableCode stockVO = shopThirdPartyTableCodes.get(i);
                list1.add(stockVO.getShopId().toString());
                list1.add(stockVO.getShopName());
                list1.add(stockVO.getTableName());
                list1.add(stockVO.getTableCodeurl());
                data.add(list1);
            }

            ExcelUtil.getHSSFWorkbook(0, "桌台码", headers, null, data, hssfWorkbook);

            //这种为直接在浏览器下载
            httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
               //attachment为以附件方式下载
                httpServletResponse.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(
                        fileName + ".xls",
                               "utf-8"));
                httpServletResponse.setHeader("Cache-Control", "No-cache");
              httpServletResponse.flushBuffer();
            hssfWorkbook.write(httpServletResponse.getOutputStream());
            hssfWorkbook.close();

            //这种为上传到服务器上的某个地方，然后返回保存的路径
            //hssfWorkbook.write(out);
            //hssfWorkbook.write(saveFile);
            //out.close();
            //上传
            //String upload = UploadUtil.customizeFileNameUpload(saveFile);
            //return BusinessWrapper.successWithBody(upload);
        } catch (Exception ex) {
            ex.printStackTrace();
            //return BusinessWrapper.failWithMessage(ex.getMessage());
        }

    }

}
